package cn.xmkeshe.controller;

import cn.xmkeshe.back.ILineServiceBack;
import cn.xmkeshe.model.Line;
import cn.xmkeshe.utils.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class LineController {

  @Resource
  private ILineServiceBack iLineServiceBack;


//  添加线路
  @RequestMapping("/pages/back/line/addPre")
  @Log(operationName = "添加线路页面",operationType = "管理员")
  public String addPre(){
    return "/pages/back/line/add";
  }

  @RequestMapping("/pages/back/line/add")
  @ResponseBody
  @Log(operationName = "添加线路信息",operationType = "系统")
  public String add(Line line){
    String flag = "error";
    line.setLid(UUID.randomUUID().toString().substring(5,9));
    line.setStatus(1);
    if(iLineServiceBack.insert(line)){
      return "success";
    }
    return flag;
  }

//  查询线路
  @RequestMapping("/pages/back/line/listSplitPre")
  @Log(operationName = "查询线路页面",operationType = "管理员")
  public String listSplitPre(){
    return "/pages/back/line/list";
  }

  @RequestMapping("/pages/back/line/listSplit")
  @ResponseBody
  @Log(operationName = "线路数据",operationType = "系统")
  public Map<String, Object> listSplit(Line line, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
//      System.out.println(currentPage);
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLineServiceBack.listSplit(currentPage,lineSize);
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

  @RequestMapping("/pages/back/line/edit")
  @ResponseBody
  @Log(operationName = "修改线路信息",operationType = "管理员")
  public String edit(String lid){
    int status;
    Line line = iLineServiceBack.findById(lid);
    int status1 = line.getStatus();
    if (status1 == 1){
      status = 2;
    }else{
      status = 1;
    }
    String flag = "error";
    if(iLineServiceBack.edit(lid,status)){
      return "success";
    }
    return flag;
  }

//  所有线路
  @RequestMapping("/pages/back/line/list")
  @ResponseBody
  @Log(operationName = "所有线路",operationType = "系统")
  public List<Line> findAll(){
    return iLineServiceBack.list();
  }

  //模糊查询
  @RequestMapping("/pages/back/line/findByName")
  @ResponseBody
  @Log(operationName = "模糊查询线路",operationType = "管理员")
  public Map<String, Object> findByName(String name, HttpServletRequest request){
    int currentPage = 1;
    int lineSize = 10;
    try{
      currentPage = Integer.parseInt(request.getParameter("page"));
    }catch (Exception e){}
    try{
      lineSize = Integer.parseInt(request.getParameter("limit"));
    }catch (Exception e){}
    Map<String, Object> map = iLineServiceBack.findByName(currentPage,lineSize,"%" + name + "%");
//    System.out.println(map);
    map.put("code",0);//解析接口状态，必须这样写
    map.put("msg","加载中");//解析提示文本
    map.put("data",map.get("data"));//    解析数据列表
    map.put("count",map.get("count"));//    解析数据长度
    return map;
  }

}
